package com.chhd.cloud.common.config;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.extension.api.R;
import com.chhd.cloud.common.entity.SmsForm;
import com.xf.common.core.domain.DmpResult;
import com.xf.common.core.entity.FebsResponse;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author xufeng
 */
@Log4j2
@Configuration
public class SendSmsConfig {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Value("${aliyun.sms.AccessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.sms.AccessKeySecret}")
    private String accessKeySecret;

    @Value("${spring.profiles.active}")
    private String profilesActive;

    public DmpResult send(SmsForm smsForm) {
        DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        // 为变量赋随机值1000-9999
        Random ne = new Random();
        int number = ne.nextInt(9999 - 1000 + 1) + 1000;
        log.info(smsForm.getPhoneNumbers() + "的验证码是" + number);
        stringRedisTemplate.opsForValue().set(smsForm.getPhoneNumbers() + "sms", String.valueOf(number), 300,
                TimeUnit.SECONDS);
        /*
         * if (!StringUtils.isEmpty(profilesActive) && profilesActive.equals("dev")) {
         * return R.success(); }
         */
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        // 必填:待发送手机号
        request.putQueryParameter("PhoneNumbers", smsForm.getPhoneNumbers());
        // 必填:短信签名-可在短信控制台中找到 "球迷巡礼"
        request.putQueryParameter("SignName", smsForm.getSignName());
        // 必填:短信模板-可在短信控制台中找到 "SMS_171187342"
        request.putQueryParameter("TemplateCode", smsForm.getTemplateCode());
        // 可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 "{\"code\":1233}"
        request.putQueryParameter("TemplateParam", "{\"code\":" + number + "}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            JSONObject jsonObject = JSONObject.parseObject(response.getData());
            if ("OK".equals(jsonObject.get("Code"))) {
                return DmpResult.success();
            } else {
                log.info("send sms response data:{}", response.getData());
                return DmpResult.success((String) jsonObject.get("Message"));
            }
        } catch (ClientException e) {
            log.info("send sms response data:{}", e.getErrMsg());
            e.printStackTrace();
        }
        return DmpResult.failed("发送失败");
    }

}
